<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_page_w_grid_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/grids/_ci/ci_attachments/ci_attachments_grid_ctrl.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_composite_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_attachment_dbc.class.php');


class CI_Attachments_WP extends TCMS_Admin_Page_W_Grid_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_ci/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('vpage');
		$this->add_transit_var('category');
		$this->add_transit_var('ret');
		$this->add_transit_var('vpt_context');
	}


	protected function init_views() {
		$v = $this->create_view();
		$this->add_view($v);
	}


	protected function create_grid_ctrl() {
		$tvm = $this->get_tvm();

		$context = $this->get__context();
		if (!$context->exists_in_get('ci')) {
			throw new Tangra_Module_Exception('Expecting ci parameter.');
		}

		$ret = new CI_Attachments_Grid_Ctrl('ci_attachments', $tvm, Tangra_Parameter_Method::GET, $context->get_from_get('ci'));
		$ret->set_additional_parameters(array('ci' => $context->get_from_get('ci')));
		return $ret;
	}


	protected function default_event($page_num = NULL) {
		$this->export('vpt_context', $this->get_transit_var('vpt_context', true));

		$dbc = $this->get_dbc();

		$context = $this->get__context();

		if ($context->exists_in_get('remove')) {
			$ci_attachment_id = $context->get_from_get('remove');

			$ci_attachment = new TCMS_CI_Attachment_DBC();
			if ($ci_attachment->load_by_id($dbc, $ci_attachment_id)) {
				try {
					$dbc->start_trans();
					$this->create_ci_mod($dbc, $ci_attachment->get_ci());
					TCMS_CI_Attachment_DBC::delete($dbc, $ci_attachment_id);
					$dbc->complete_trans();
				} catch (Exception $e) {
					$dbc->fail_trans();
					$dbc->complete_trans();
					throw $e;
				}
			}
		}


		if ($context->exists_in_get('ci')) {
			$ci_id = $context->get_from_get('ci');
			$this->export('ci_id', $ci_id);

			$ci = new TCMS_CI_Composite_DBC($this->_config->get_hidden_dir().'inc/');
			if (!$ci->load_by_id($dbc, $ci_id)) {
				throw new Tangra_Module_Exception('Cannot load attachments for this ci item because ci item does not exists. ID = '.$ci_id);
			}
		} else {
			throw new Tangra_Module_Exception('Expecting ci parameter.');
		}



		$this->export('ci_title', $ci->get_as_short_plain_text());

		$vpage_id = $this->get_transit_var('vpage', true);
		if ($vpage_id) {
			$vpage = new TCMS_VPage_DBC();
			if ($vpage->load_by_id($dbc, $vpage_id)) {
				$this->export('vpage', $vpage);
			}
		}

		$ret = $this->get_transit_var('ret', true);
		if ($ret) {
			$this->export('ret', urldecode($ret));
		}

		return parent::default_event($page_num);
	}


	private function create_ci_mod(DB_Connection $dbc, $ci_id) {
		$ci_mod = new TCMS_CI_Mod_DBC();
		$ci_mod->set_ci($ci_id);
		$ci_mod->set_modified_by($this->get_user()->get_id());
		$ci_mod->save($dbc);
	}
}